/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.crud.Impl;

import com.startup.musicstore.app.factory.AppFactory;
import com.startup.musicstore.model.*;
import com.startup.musicstore.services.crud.AlbumsBoughtService;
import com.startup.musicstore.services.crud.CustomerCrudService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author given
 */
public class AlbumsBoughtServiceImpl implements AlbumsBoughtService{
    private static ApplicationContext ctx;
    
    @Override
    public List<Album> getAlbumsBoughtByGender(String gender) {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");
        CustomerCrudService customerCrudService = (CustomerCrudService)ctx.getBean("customerCrudService");
        List<Customer> allCustomers = customerCrudService.findAll();
        List<Customer> customers = AppFactory.getCustomers();
        List<Album> albums = AppFactory.getAlbums();
        for(Customer c: allCustomers){
            Demographic demographic = c.getDemographic();
            if(gender.equals(demographic.getGender())){
                customers.add(c);                
            }
        }
        
        for(Customer c: customers){
            List<Orders> orders = c.getOrders();
            for(Orders o: orders){
                List<OrderItem> orderItems = o.getOrderItems();
                for(OrderItem oi: orderItems){
                    albums.add(oi.getAlbum());
                }
            }
        }
        
        return albums;
    }
    
}
